package com.sskj.fabi.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.inputfilter.MoneyValueFilter;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.RxSchedulersHelper;
import com.sskj.common.util.ToastUtil;
import com.sskj.fabi.R;
import com.sskj.lib.bean.TabItem;
import com.sskj.lib.bean.enums.FabiBuySellEnum;
import com.sskj.lib.bean.enums.PayTypeEnum;
import com.sskj.lib.box.verifier.ToastNextInputs;
import com.sskj.lib.util.CoinUtil;
import com.sskj.lib.util.ImageUtil;
import com.sskj.lib.util.SchemeUtil;
import com.sskj.lib.widget.RudessMaterialDialog;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;

public class TipFabiUtil {

    private static boolean isPrice;
    private static String resultNum;
    private static String resultRMB;
    private static String type = "1";//1法币计价 2购买数量
    private static Disposable timerDispo;
    private static PayTypeEnum payTypeEnum;
    private static int checkId;

    /**
     * 订单列表筛选
     *
     * @param activity
     * @return
     */
    public static BottomSheetDialog showQuickPayType(Activity activity, OnInputList onInputList) {

        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(activity);
        View customView = LayoutInflater.from(activity).inflate(R.layout.fabi_dialog_pay_type, null, false);
        bottomSheetDialog.setContentView(customView);
        bottomSheetDialog.setCancelable(true);
        bottomSheetDialog.setCanceledOnTouchOutside(true);
        ClickUtil.click(customView.findViewById(R.id.llAlipay), () -> {
            onInputList.onSure(PayTypeEnum.ALIPAY.getQuickPayType());
        });
        ClickUtil.click(customView.findViewById(R.id.llBank), () -> {
            onInputList.onSure(PayTypeEnum.BANK.getQuickPayType());
        });
        ClickUtil.click(customView.findViewById(R.id.ivClose), () -> {
            bottomSheetDialog.dismiss();
        });

        return bottomSheetDialog;
    }

    /**
     * 订单列表筛选
     *
     * @param activity
     * @return
     */
    public static BottomSheetDialog showFilterBottom(Activity activity, String title, List<String> list, OnInputList onInputList) {

        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(activity);
        View customView = LayoutInflater.from(activity).inflate(R.layout.fabi_dialog_filter_bottom, null, false);
        TextView tvTitle = customView.findViewById(R.id.tvTitle);
//        MyRadioGroup myRadioGroup = customView.findViewById(R.id.myRadioGroup);
        RecyclerView recyclerView = customView.findViewById(R.id.recyclerView);
        LinearLayout llRoot = customView.findViewById(R.id.llRoot);
        tvTitle.setText(title);

        recyclerView.setLayoutManager(new GridLayoutManager(activity, 4));
        checkId = -1;
        SlimAdapter.create().register(R.layout.fabi_recy_item_filter, new SlimInjector<String>() {
            @Override
            public void onInject(String data, IViewInjector injector, List list) {
                injector.itemView(view -> {
                    ViewGroup viewGroup = (ViewGroup) view;


                    Button childAt = (Button) (viewGroup.getChildAt(0));
                    childAt.setId(injector.pos());
                    childAt.setText(data);
                    childAt.setOnClickListener(v -> {
                        if (checkId == childAt.getId()) {
                            checkId = -1;
                            childAt.setBackground(ContextCompat.getDrawable(App.INSTANCE, R.drawable.fabi_shape_filter_uncheck));
                            childAt.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.fabiTextContent));
                        } else {
                            if (checkId != -1) {
                                Button checkButton = (Button) recyclerView.findViewById(checkId);
                                checkButton.setBackground(ContextCompat.getDrawable(App.INSTANCE, R.drawable.fabi_shape_filter_uncheck));
                                checkButton.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.fabiTextContent));
                            }

                            childAt.setBackground(ContextCompat.getDrawable(App.INSTANCE, R.drawable.fabi_shape_filter_check));
                            childAt.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.fabiSelect));
                            checkId = childAt.getId();
                        }
                        onInputList.onSure(checkId + "");

                    });
                });
            }
        }).attachTo(recyclerView).updateData(list);
//        llRoot.post(() -> {
//            llRoot.removeViewAt(0);
//            MyRadioGroup.LayoutParams layoutParams = new MyRadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//            myRadioGroup.addView(recyclerView, 0, layoutParams);
//        });
//        myRadioGroup.setOnCheckedChangeListener((group, checkedId) -> {
//            onInputList.onSure(checkedId + "");
//        });

        bottomSheetDialog.setContentView(customView);
        bottomSheetDialog.setCancelable(true);
        bottomSheetDialog.setCanceledOnTouchOutside(true);
        return bottomSheetDialog;

    }

    /**
     * 快捷买币确定弹窗
     *
     * @param activity
     * @return
     */
    public static BottomSheetDialog showQuickBuy(Activity activity, PayTypeEnum payType, String price, String num, String money, OnInputList onInputList) {

        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(activity);
        View customView = LayoutInflater.from(activity).inflate(R.layout.fabi_dialog_quick_buy, null, false);
        TextView tvPayType = customView.findViewById(R.id.tvPayType);
        TextView tvPrice = customView.findViewById(R.id.tvPrice);
        TextView tvNum = customView.findViewById(R.id.tvNum);
        TextView tvMoney = customView.findViewById(R.id.tvMoney);
        TextView tvClose = customView.findViewById(R.id.tvClose);
        tvPayType.setText(payType.getName());
        tvPrice.setText(price);
        tvNum.setText(num);
        tvMoney.setText(money);
        Button btSubmit = customView.findViewById(R.id.btSubmit);


        ClickUtil.click(btSubmit, () -> {
            bottomSheetDialog.dismiss();
            onInputList.onSure();
        });
        ClickUtil.click(tvClose, bottomSheetDialog::dismiss);
        bottomSheetDialog.setContentView(customView);
        bottomSheetDialog.setCancelable(true);
        bottomSheetDialog.setCanceledOnTouchOutside(true);
        return bottomSheetDialog;

    }

    /**
     * 拨打电话
     *
     * @param activity
     * @return
     */
    public static MaterialDialog showCall(Activity activity, String phone, OnInputList onTipSure) {
        MaterialDialog dialog = new RudessMaterialDialog.Builder(activity)
                .customView(R.layout.fabi_dialog_phone, false)
                .autoDismiss(true)
                .cancelable(true)
                .show();
        View customView = dialog.getCustomView();
        ((ViewGroup) (customView.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        TextView tvContent = customView.findViewById(R.id.tvContent);
        Button btSure = customView.findViewById(R.id.btSure);
        Button btCancel = customView.findViewById(R.id.btCancel);
        ClickUtil.click(btCancel, dialog::dismiss);
        tvContent.setText(phone);
        btSure.setText(App.INSTANCE.getString(R.string.fabitipFabiUtil1));

        ClickUtil.click(btSure, () -> {
            dialog.dismiss();
            onTipSure.onSure(phone);
        });
        return dialog;

    }

    /**
     * 筛选。右侧
     *
     * @param activity
     * @return
     */
    public static MaterialDialog showFilter(Activity activity, OnInputList onInputList) {
        MaterialDialog dialog = new RudessMaterialDialog.Builder(activity)
                .customView(R.layout.fabi_dialog_filter, false)
                .autoDismiss(true)
                .cancelable(true)
                .show();
        WindowManager windowManager = activity.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.width = (int) (display.getWidth()) / 3 * 2; //设置宽度
        lp.height = (int) (display.getHeight()); //设置宽度
        lp.gravity = Gravity.RIGHT;
        dialog.getWindow().setAttributes(lp);
        View customView = dialog.getCustomView();
        EditText etMoney = customView.findViewById(R.id.etMoney);
        Button btReset = customView.findViewById(R.id.btReset);
        Button btSubmit = customView.findViewById(R.id.btSubmit);
        RadioGroup rgFilter = customView.findViewById(R.id.rgFilter);
        RadioButton rbAlipay = customView.findViewById(R.id.rbAlipay);
        RadioButton rbBank = customView.findViewById(R.id.rbBank);
        RadioButton rbWX = customView.findViewById(R.id.rbWX);
        ClickUtil.click(btReset, () -> {
            etMoney.setText("");
//            rbAlipay.setChecked(false);
//            rbWX.setChecked(false);
//            rbBank.setChecked(false);
            rgFilter.clearCheck();
        });
        payTypeEnum = null;
        etMoney.setFilters(new InputFilter[]{new MoneyValueFilter(2)});
        ClickUtil.click(btSubmit, () -> {
            ArrayList<PayTypeEnum> payTypeEnums = new ArrayList<>();
            String payType = null;
            if (rbAlipay.isChecked()) {
                payTypeEnums.add(PayTypeEnum.ALIPAY);
                payType = PayTypeEnum.ALIPAY.getType();
            }
            if (rbWX.isChecked()) {
                payTypeEnums.add(PayTypeEnum.WX);
                payType = PayTypeEnum.WX.getType();

            }
            if (rbBank.isChecked()) {
                payTypeEnums.add(PayTypeEnum.BANK);
                payType = PayTypeEnum.BANK.getType();
            }
            onInputList.onSure(etMoney.getText().toString(), payType);
            dialog.dismiss();
        });
        return dialog;

    }

    /**
     * 交易
     *
     * @param activity
     * @param isBuy       购买、出售
     * @param price       单价
     * @param minLimit    最小限额
     * @param maxLimit    最大限额
     * @param leftNum     剩余数量
     * @param onInputList
     * @return
     */
    public static BottomSheetDialog showTrade(Activity activity, boolean isBuy, String price, String minLimit, String maxLimit, String leftNum, OnInputList onInputList) {

        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(activity);
        View rootView = LayoutInflater.from(activity).inflate(R.layout.fabi_bottom_sheet_trade, null, false);
        TextView tvTitle = rootView.findViewById(R.id.tvTitle);
        TextView tvPrice = rootView.findViewById(R.id.tvPrice);
        CommonTabLayout commonTabLayout = rootView.findViewById(R.id.commonTabLayout);
        EditText etNum = rootView.findViewById(R.id.etNum);
        EditText etPwd = rootView.findViewById(R.id.etPwd);
        TextView tvUnit = rootView.findViewById(R.id.tvUnit);
        TextView tvAll = rootView.findViewById(R.id.tvAll);
        TextView tvUSDT = rootView.findViewById(R.id.tvUSDT);
        TextView tvRMB = rootView.findViewById(R.id.tvRMB);
        TextView tvUSDTTitle = rootView.findViewById(R.id.tvUSDTTitle);
        TextView tvLimitMoney = rootView.findViewById(R.id.tvLimitMoney);
        Button btOrder = rootView.findViewById(R.id.btOrder);
        Button btAutoCancel = rootView.findViewById(R.id.btAutoCancel);
        isPrice = true;
        tvAll.setText(String.format(App.INSTANCE.getString(R.string.fabifabi_activity_order460)));
        tvTitle.setText(String.format("%sUSDT", FabiBuySellEnum.getTitle(isBuy)));
        tvPrice.setText(String.format(App.INSTANCE.getString(R.string.fabitipFabiUtil3), CoinUtil.keepRMB(price)));
        etNum.setHint(String.format(App.INSTANCE.getString(R.string.fabitipFabiUtil4), FabiBuySellEnum.getTitle(isBuy), isPrice ? App.INSTANCE.getString(R.string.fabitipFabiUtil5) : App.INSTANCE.getString(R.string.fabitipFabiUtil6)));
        tvUSDTTitle.setText(String.format(App.INSTANCE.getString(R.string.fabitipFabiUtil7), FabiBuySellEnum.getTitle(isBuy)));
        tvLimitMoney.setText(String.format(App.INSTANCE.getString(R.string.fabitipFabiUtil8), CoinUtil.keepRMB(minLimit), CoinUtil.keepRMB(maxLimit)));
        tvUnit.setText(isPrice ? "CNY" : "USDT");
        ArrayList<CustomTabEntity> tabItems = new ArrayList<>();
        tabItems.add(new TabItem(App.INSTANCE.getString(R.string.fabitipFabiUtil9), 0, 0));
        tabItems.add(new TabItem(String.format(App.INSTANCE.getString(R.string.fabitipFabiUtil10), FabiBuySellEnum.getTitle(isBuy)), 0, 0));
        commonTabLayout.setTabData(tabItems);
        resultNum = "0";
        resultRMB = "0";
        if (isBuy) {
            etPwd.setVisibility(View.GONE);
        }
        DisposUtil.close(timerDispo);
        timerDispo = Flowable.intervalRange(1, 45, 0, 1, TimeUnit.SECONDS)
                .compose(RxSchedulersHelper.transformer())
                .map(aLong -> 45 - aLong)
                .subscribe(aLong -> {
                    btAutoCancel.setText(String.format(App.INSTANCE.getString(R.string.fabitipFabiUtil11), aLong + ""));
                }, e -> {
                }, () -> {
                    bottomSheetDialog.dismiss();
                });
        bottomSheetDialog.setOnDismissListener(dialog -> {
            DisposUtil.close(timerDispo);
        });
        commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                etNum.setText("");
                if (position == 0 && !isPrice) {
                    isPrice = true;
                    etNum.setHint(String.format(App.INSTANCE.getString(R.string.fabitipFabiUtil12), FabiBuySellEnum.getTitle(isBuy)));
                    tvUnit.setText("CNY");
                    type = "1";
                } else if (position == 1 && isPrice) {
                    isPrice = false;
                    etNum.setHint(String.format(App.INSTANCE.getString(R.string.fabitipFabiUtil13), FabiBuySellEnum.getTitle(isBuy)));
                    tvUnit.setText("USDT");
                    type = "2";
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        ClickUtil.click(tvAll, () -> {
            String num = calculateAll(isPrice, price, leftNum, maxLimit);
            etNum.setText(num);

        });
        ClickUtil.click(btAutoCancel, () -> {
            bottomSheetDialog.dismiss();
        });
        RxTextView.afterTextChangeEvents(etNum)
                .subscribe(textEdit -> {
                    String text = textEdit.editable().toString();
                    if (!TextUtils.isEmpty(text)) {
                        if (isPrice) {
                            BigDecimal usdtBig = new BigDecimal(text).divide(new BigDecimal(price), 8, BigDecimal.ROUND_DOWN);
                            String usdt = CoinUtil.keepUSDT(usdtBig.toPlainString());
                            tvUSDT.setText(String.format("%s USDT", CoinUtil.keepUSDT(usdt)));
                            resultNum = usdt;
                            tvRMB.setText(String.format("￥%s", CoinUtil.keepRMB(text)));
                            resultRMB = text;
                        } else {
                            BigDecimal priceBig = new BigDecimal(text).multiply(new BigDecimal(price));
                            String rmb = CoinUtil.keepRMB(priceBig.toPlainString());
                            resultNum = text;
                            tvUSDT.setText(String.format("%s USDT", CoinUtil.keepUSDT(text)));
                            tvRMB.setText(String.format("￥%s", CoinUtil.keepRMB(rmb)));
                            resultRMB = rmb;
                        }
                    } else {
                        resultNum = "0";
                        tvUSDT.setText("0 USDT");
                        tvRMB.setText("￥0.00");
                    }
                });
        ClickUtil.click(btOrder, () -> {
            if (TextUtils.isEmpty(etNum.getText())) {
                ToastUtil.showShort(etNum.getHint());
                return;
            }
            if (Double.valueOf(resultRMB) > Double.valueOf(maxLimit)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.fabitipFabiUtil14));
                return;
            }
            if (Double.valueOf(resultRMB) < Double.valueOf(minLimit)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.fabitipFabiUtil15));
                return;
            }
            if (Double.valueOf(resultNum) > Double.valueOf(leftNum)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.fabitipFabiUtil16));
                return;
            }
            if (!isBuy) {
                if (TextUtils.isEmpty(etPwd.getText())) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.fabitipFabiUtil17));
                    return;
                }
            }
            if (onInputList != null) {
                onInputList.onSure(resultNum, resultRMB, etPwd.getText().toString(), type);
            }

        });

        bottomSheetDialog.setContentView(rootView);
        bottomSheetDialog.setCancelable(true);
        bottomSheetDialog.setCanceledOnTouchOutside(true);
        return bottomSheetDialog;

    }

    /**
     * 发布订单弹窗
     *
     * @param context
     * @param isBuy
     * @param price
     * @param num
     * @param onInputList
     * @return
     */
    public static MaterialDialog showPublish(Context context, boolean isBuy, String price, String num, OnInputList onInputList) {
        MaterialDialog dialog = new RudessMaterialDialog.Builder(context)
                .customView(R.layout.fabi_dialog_publish_confirm, false)
                .autoDismiss(false)
                .cancelable(false)
                .show();
        View view = dialog.getCustomView();
        ((ViewGroup) (view.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (view.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (view.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (view.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (view.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((TextView) view.findViewById(R.id.tv_price)).setText(String.format("%s CNY", CoinUtil.keepRMB(price)));
        ((TextView) view.findViewById(R.id.tv_amount)).setText(String.format("%s USDT", CoinUtil.keepUSDT(num)));
        String money = CoinUtil.keepRMB(new BigDecimal(num).multiply(new BigDecimal(price)).toPlainString());
        ((TextView) view.findViewById(R.id.tv_money)).setText(String.format("%s CNY", CoinUtil.keepUSDT(money)));
        ((TextView) view.findViewById(R.id.tv_price_left)).setText(String.format(App.INSTANCE.getString(R.string.fabitipFabiUtil18), FabiBuySellEnum.getTitle(isBuy)));
        ((TextView) view.findViewById(R.id.tv_amount_left)).setText(String.format(App.INSTANCE.getString(R.string.fabitipFabiUtil10), FabiBuySellEnum.getTitle(isBuy)));
        ((TextView) view.findViewById(R.id.tv_money_left)).setText(String.format(App.INSTANCE.getString(R.string.fabitipFabiUtil19), FabiBuySellEnum.getTitle(isBuy)));
        EditText et_pwd = view.findViewById(R.id.et_pwd);
        ClickUtil.click(view.findViewById(R.id.tvCancel), () -> {
            dialog.dismiss();
        });
        ClickUtil.click(view.findViewById(R.id.tvSure), () -> {
            if (TextUtils.isEmpty(et_pwd.getText().toString())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.fabitipFabiUtil17));
                return;
            }
            if (!CheckUtil.isPayPwd(et_pwd.getText().toString())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.fabitipFabiUtil20));
                return;
            }
            dialog.dismiss();
            onInputList.onSure(et_pwd.getText().toString());
        });
        return dialog;
    }

    /**
     * 放币资金密码弹窗
     *
     * @param onInputList
     * @return
     */
    public static MaterialDialog sendCoin(Activity activity, OnInputList onInputList) {
        MaterialDialog dialog = new RudessMaterialDialog.Builder(activity)
                .customView(R.layout.fabi_dialog_send_coin, false)
                .autoDismiss(false)
                .cancelable(false)
                .show();
        View view = dialog.getCustomView();
        ((ViewGroup) (view.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (view.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (view.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (view.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (view.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        EditText etPwd = view.findViewById(R.id.etPwd);
        ClickUtil.click(view.findViewById(R.id.btCancel), dialog::dismiss);
        TextView mSubmit = view.findViewById(R.id.btSure);
        ToastNextInputs inputs = new ToastNextInputs();
        inputs.add(etPwd, SchemeUtil.notEmpty(etPwd), SchemeUtil.pwd());

        ClickUtil.click(mSubmit, () -> {
            String pwd = etPwd.getText().toString().trim();
            if (inputs.test()) {
                onInputList.onSure(pwd);
            }

        });
        return dialog;
    }

    /**
     * 二维码
     *
     * @param activity
     * @param title
     * @param account
     * @param url
     */
    public static void showQR(Activity activity, String title, String account, String url) {
        MaterialDialog dialog = new RudessMaterialDialog.Builder(activity)
                .customView(R.layout.fabi_dialog_erweima, false)
                .autoDismiss(true)
                .cancelable(true)
                .show();
        View view = dialog.getCustomView();
        ((ViewGroup) (view.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (view.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (view.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (view.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (view.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ImageView ivQR = view.findViewById(R.id.ivQRCode);
        TextView tvContent = view.findViewById(R.id.tvContent);
        TextView tvTitle = view.findViewById(R.id.tvTitle);
        ImageView ivClose = view.findViewById(R.id.ivClose);
        ClickUtil.click(ivClose, () -> {
            dialog.dismiss();
        });
        tvTitle.setText(title);
        tvContent.setText(account);
        ImageUtil.bitmap(url, bitmap -> {
            ivQR.setImageBitmap(bitmap);
//            ClickUtil.click(tvContent, () -> {
//                new RxPermissions(activity).request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
//                        .subscribe(isGrant -> {
//                            if (isGrant) {
//                                if (ImgUtil.saveImageToGallery(activity, bitmap) == null) {
//                                    ToastUtil.showShort(getString(R.string.fabitipFabiUtil21));
//                                }
//                            } else {
//                                PermissionTipUtil.tip(activity, App.INSTANCE.getString(R.string.fabitipFabiUtil22));
//                            }
//                        });
//            });
        });

//        tvContent.setText(getString(R.string.fabitipFabiUtil23));
    }

    /**
     * 申诉
     *
     * @param activity
     * @param onInputList
     * @return
     */
    public static MaterialDialog showAppealTip(Activity activity, OnInputList onInputList) {
        MaterialDialog dialog = new RudessMaterialDialog.Builder(activity)
                .customView(R.layout.fabi_dialog_appeal, false)
                .autoDismiss(true)
                .cancelable(true)
                .build();
        View customView = dialog.getCustomView();
        ((ViewGroup) (customView.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        EditText etReason = customView.findViewById(R.id.etReason);
        Button btSure = customView.findViewById(R.id.btSure);
        Button btCancel = customView.findViewById(R.id.btCancel);
        ClickUtil.click(btCancel, dialog::dismiss);

        ClickUtil.click(btSure, () -> {
            if (TextUtils.isEmpty(etReason.getText())) {
                ToastUtil.showShort(etReason.getHint());
                return;
            }
            onInputList.onSure(etReason.getText().toString());
        });
        return dialog;
    }

    public interface OnInputList {
        void onSure(String... str);

    }


    /**
     * 根据法币计价和出售数量计算总的数量与价格
     */
    private static String calculateAll(boolean isPrice, String price, String leftNum, String maxLimit) {

        String num = "0.0000";
        BigDecimal allMoney = new BigDecimal(leftNum).multiply(new BigDecimal(price));//最多购买金额
        BigDecimal allNum = new BigDecimal(maxLimit).divide(new BigDecimal(price), 8, BigDecimal.ROUND_DOWN);

        if (isPrice) {
            if (!TextUtils.isEmpty(price)) {
                double buyPrice = Math.min(Double.valueOf(maxLimit), allMoney.doubleValue());
                return CoinUtil.keepRMB(buyPrice);
            }
        } else {
            if (!TextUtils.isEmpty(price)) {
                double buyNum = Math.min(allNum.doubleValue(), Double.valueOf(leftNum));
                return CoinUtil.keepUSDT(buyNum);
            }
        }
        return num;
    }
}
